package com.itheima.threadpool;

import java.util.concurrent.*;

/**
 * @author Anomaly
 * @date 2023/2/10 23:16
 */
public class ThreadPoolDemo2 {
    public static void main(String[] args) {
        ExecutorService pool = new ThreadPoolExecutor(3, 5, 6, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        Future<String> f1 = pool.submit(new MyCallable());
        Future<String> f2 = pool.submit(new MyCallable());
        Future<String> f3 = pool.submit(new MyCallable());
        Future<String> f4 = pool.submit(new MyCallable());
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "子线程执行输出：" + i);
        }
        return Thread.currentThread().getName() + "线程执行完毕";
    }
}